package com.group.view;

import com.group.controller.StudentController;
import com.group.util.*;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

/**
 * @author lenovo
 */
public class ExportPanel extends JPanel {
    JLabel status = new JLabel();
    public ExportPanel() {
        setLayout(new GridLayout(7, 1));
        JButton exportBtn = new JButton("导出成绩到 TXT");
        add(exportBtn);
        JButton exportBtnExcel = new JButton("导出成绩到 Excel");
        add(exportBtnExcel);
        JButton inTest = new JButton("导入测试（会清除已有数据）");
        add(inTest);
        JButton deleteAll = new JButton("清空学生");
        add(deleteAll);
        add(status);

        exportBtn.addActionListener(e -> {
            try {
                StudentController.saveToFileOrder("成绩表.txt");
                status.setText("导出成功 -> 成绩表.txt");
            } catch (IOException ex) {
                status.setText("导出失败");
            }
        });

        exportBtnExcel.addActionListener(e -> {
            try {
                ExcelExportUtil.exportToExcel("成绩表.xlsx");
                status.setText("导出成功 -> 成绩表.xlsx");
            } catch (Exception ex) {
                status.setText("导出失败");
            }
        });

        inTest.addActionListener(e -> {
            try {
                StudentDataLoader.generateAndSave("saveStudents.txt", 100000);
                StudentDataLoader.loadFromFile("saveStudents.txt");
                StudentController.saveToFile("txt.txt");
                StudentDataLoader.exportToExcel("excel.xlsx", StudentController.getAll());
                status.setText("导入成功");
            } catch (Exception ex) {
                status.setText("导入失败");
            }
        });

        deleteAll.addActionListener(e -> {
            try {
                StudentController.deleteAll("saveStudents.txt");
                status.setText("清空成功");
            } catch (Exception ex) {
                status.setText("清空失败");
            }
        });
    }
}